package org.galaxyworld.flycode.jobs;

import org.apache.commons.lang3.StringUtils;
import org.galaxyworld.flycode.config.ConfigManager;
import org.galaxyworld.flycode.config.JdbcConfigData;
import org.galaxyworld.flycode.jobs.AbstractDatabaseGenerator;
import org.galaxyworld.flycode.jobs.Service;

import java.sql.*;

public class ServiceGenerator extends AbstractDatabaseGenerator {

    @Override
    protected void doWithConnection(Connection connection) throws SQLException {
        JdbcConfigData jdbcConfigData = ConfigManager.getConfig(JdbcConfigData.class);
        DatabaseMetaData dmd = connection.getMetaData();
        ResultSet tables = dmd.getTables(
                connection.getCatalog(), StringUtils.upperCase(jdbcConfigData.getUserName()),
                null, new String[]{"TABLE", "VIEW"});
        showMessage("Read done.", "Start generating...");
        while (tables.next()) {
            Service service = new Service();
            String tableName = tables.getString("TABLE_NAME");
            service.setTableName(tableName);
            service.saveToFile();
            showMessage(tableName + " created.");
        }
        tables.close();
    }
}
